<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;
use app\admin\model\store\Shop;
use app\admin\model\service\Service;
use app\admin\model\staff\Staff;
use app\admin\model\wanlshop\Order;
use think\Session;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');

        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime; ) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);

        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        //1

        $shop_id = Session::get('shop');
        if ($shop_id == 1) {
            $where2 = [];
        } else {
            $where2['o.shop_id'] = $shop_id;
        }

        $data1 = Db::name("wanlshop_order")->alias('o')
            ->where('o.createtime', 'between time', [$starttime, $endtime])
            //->where('o.shop_id','=',$shop_id)
            ->where($where2)
            ->where('o.state', 'in', '2,3,4,5,6')
            ->where('g.gdtype', '=', '0')
            ->join('wanlshop_order_goods g', 'g.order_id=o.id', 'left')
            ->where('g.id', '>', 0)
            ->field('DATE_FORMAT(FROM_UNIXTIME(o.createtime), "%Y-%m-%d") AS o_date,sum(g.actual_payment * g.number) as o_sum,count(distinct o.id) as o_no')
            ->group('o_date')
            ->select();

        $data2 = Db::name("wanlshop_order")->alias('o')
            ->where('o.createtime', 'between time', [$starttime, $endtime])
            // ->where('o.shop_id','=',$shop_id)
            ->where($where2)
            ->where('o.state', 'in', '2,3,4,5,6')
            ->where('g.gdtype', '=', '1')
            ->join('wanlshop_order_goods g', 'g.order_id=o.id', 'left')
            ->where('g.id', '>', 0)
            ->field('DATE_FORMAT(FROM_UNIXTIME(o.createtime), "%Y-%m-%d") AS o_date,sum(g.actual_payment * g.number) as o_sum,count(distinct o.id) as o_no')
            ->group('o_date')
            ->select();

        $data3 = Db::name("wanlshop_order")->alias('o')
            ->where('o.createtime', 'between time', [$starttime, $endtime])
            //->where('o.shop_id','=',$shop_id)
            ->where($where2)
            ->where('o.state', 'in', '2,3,4,5,6')
            //->where('g.gdtype','=','1')
            ->join('wanlshop_order_item g', 'g.order_id=o.id', 'left')
            ->where('g.id', '>', 0)
            ->field('DATE_FORMAT(FROM_UNIXTIME(o.createtime), "%Y-%m-%d") AS o_date,sum(g.actual_price) as o_sum,count(distinct o.id) as o_no')
            ->group('o_date')
            ->select();

        $data4 = Db::name("wanlshop_order")->alias('o')
            ->where('o.createtime', 'between time', [$starttime, $endtime])
            // ->where('o.shop_id','=',$shop_id)
            ->where($where2)
            ->where('o.state', 'in', '2,3,4,5,6')
            //->where('g.gdtype','=','0')
            //->join('wanlshop_order_goods g','g.order_id=o.id','left')
            ->field('DATE_FORMAT(FROM_UNIXTIME(o.createtime), "%Y-%m-%d") AS o_date,count(distinct o.id) as o_no')
            ->group('o_date')
            ->select();

        $orderlist = array_fill_keys($column, 0);
        $order_slist = array_fill_keys($column, 0);
        foreach ($data1 as $k => $v) {
            $orderlist[$v['o_date']] = $v['o_no'];
            $order_slist[$v['o_date']] = $v['o_sum'];
        }
        $orderlist2 = array_fill_keys($column, 0);
        $order_slist2 = array_fill_keys($column, 0);
        foreach ($data2 as $k => $v) {
            $orderlist2[$v['o_date']] = $v['o_no'];
            $order_slist2[$v['o_date']] = $v['o_sum'];
        }
        $orderlist3 = array_fill_keys($column, 0);
        $order_slist3 = array_fill_keys($column, 0);
        foreach ($data3 as $k => $v) {
            $orderlist3[$v['o_date']] = $v['o_no'];
            $order_slist3[$v['o_date']] = $v['o_sum'];
        }
        $orderlist4 = array_fill_keys($column, 0);
        foreach ($data4 as $k => $v) {
            $orderlist4[$v['o_date']] = $v['o_no'];
            //$order_slist3[$v['o_date']] = $v['o_sum'];
        }
        $orderlist5 = array_fill_keys($column, 0);
        foreach ($data4 as $k => $v) {
            $orderlist5[$v['o_date']] = $userlist[$v['o_date']] + $v['o_no'];
            //$order_slist3[$v['o_date']] = $v['o_sum'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $addonList = get_addon_list();
        $totalworkingaddon = 0;
        $totaladdon = count($addonList);
        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }
        $this->view->assign([
            'totaluser' => User::count(),
            'totaladdon' => $totaladdon,
            'totaladmin' => Admin::count(),
            'totalcategory' => \app\common\model\Category::count(),
            'todayusersignup' => User::whereTime('jointime', 'today')->count(),
            'todayuserlogin' => User::whereTime('logintime', 'today')->count(),
            'sevendau' => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
            'thirtydau' => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
            'threednu' => User::whereTime('jointime', '-3 days')->count(),
            'sevendnu' => User::whereTime('jointime', '-7 days')->count(),
            'dbtablenums' => count($dbTableList),
            'dbsize' => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'totalworkingaddon' => $totalworkingaddon,
            'attachmentnums' => Attachment::count(),
            'attachmentsize' => Attachment::sum('filesize'),
            'picturenums' => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize' => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),

            'shopsum' => Shop::count(),
            'servicesum' => Service::count(),
            'staffsum' => Staff::count(),
            'ordersum' => Order::whereTime('createtime', 'today')->where('state', 'in', '2,3,4,5,6')->sum('actual_price'),
            'shop_1' => 0,
            'shop_2' => 0,
            'service_1' => 0,
            'staff_1' => 0,
            'order_1' => 0,
            'orderuser' => 0,
        ]);


        $data11 = Db::name("wanlshop_shop")->alias('s')
            ->field('SUBSTRING_INDEX(city, "/", 1) as name, count(*) as value, "#ff0000" as color')
            ->group('name')
            ->select();



        $this->assignconfig("mapList", $data11);
        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));
        $this->assignconfig('orderdata', array_values($orderlist));
        $this->assignconfig('order_sdata', array_values($order_slist));
        $this->assignconfig('orderdata2', array_values($orderlist2));
        $this->assignconfig('order_sdata2', array_values($order_slist2));
        $this->assignconfig('orderdata3', array_values($orderlist3));
        $this->assignconfig('order_sdata3', array_values($order_slist3));
        $this->assignconfig('orderdata4', array_values($orderlist4));
        $this->assignconfig('orderdata5', array_values($orderlist5));

        return $this->view->fetch();
    }

}
